package com.qdxwx.web.controller.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.user.UserInviteService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserInvite;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserInviteController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UserInviteController.class);

    @Autowired
    private UserInviteService userInviteService;
    @Autowired
    private WebsiteProfileService websiteProfileService;

    /**
     * 我的邀请
     */
    @RequestMapping("/invites.html")
    public String myInvite(HttpServletRequest request, ModelMap mmap) {
        User user = LoginUser.getUser(request);
        if (user == null) {
            return VIEW_ERROR_403;
        }
        int currentPage = TKConvert.toInteger(request.getParameter("page"));
        PageRequest page = new PageRequest();
        page.setCurrentPage(currentPage);
        page.setPageSize(5);
        UserInvite userInvite = new UserInvite();
        userInvite.setUserId(user.getUserId());
        List<UserInvite> userInviteList = userInviteService.getList(userInvite, page);
        mmap.put("userInviteList", userInviteList);
        long total = userInviteService.count(userInvite);
        mmap.put("totalPages", page.getTotalPages(total));
        //分销配置
        Map<String, Object> invite = websiteProfileService.getMap(WebsiteProfileType.invite.toString());
        mmap.put("invite", invite);
        mmap.put("user", user);
        mmap.put("page", page);
        mmap.put("thisDomain", TKRequest.getServerUri(request));
        if (TKRequest.isMobileBrowse(request)){
            return getViewPathH5("user/uc-invite");
        }
        return getViewPath("ucenter/uc-invite");
    }
    /**
     * 查询qdx_user_invites分页列表
     */
    @RequestMapping("/invite/list.html")
    public String userInviteListPage(HttpServletRequest request, ModelMap model) {
        User user = LoginUser.getUser(request);
        if (user == null) {
            return VIEW_ERROR_403;
        }
        int currentPage = TKConvert.toInteger(request.getParameter("page"));
        PageRequest page = new PageRequest();
        page.setCurrentPage(currentPage);
        page.setPageSize(5);
        //按条件查询qdx_user_invites分页
        UserInvite userInvite = new UserInvite();
        userInvite.setUserId(user.getUserId());
        List<UserInvite> userInviteList = userInviteService.getList(userInvite, page);
        //qdx_user_invites数据
        model.put("userInviteList", userInviteList);
        //分数数据
        model.put("page", page);
        return getViewPath("ucenter/invite-list");
    }
}



